package com.smt.common.aspect;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.smt.common.utils.BillUtils;
import com.smt.common.utils.Constant;
import com.smt.modules.app.annotation.Reward;
import com.smt.modules.bus.entity.BillEntity;
import com.smt.modules.bus.entity.MemberUserEntity;
import com.smt.modules.bus.service.BillService;
import com.smt.modules.bus.service.MemberUserService;
import com.smt.modules.bus.vo.CompanyVo;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhangyi
 * @date 2018/10/17 14:55
 */
@Aspect
@Configuration
@Slf4j
public class RewardAspect {

    @Autowired
    private BillService billService;
    @Autowired
    private MemberUserService memberUserService;

    @Pointcut("@annotation(com.smt.modules.app.annotation.Reward)")
    public void operateCut(){}

    @After("operateCut()")
    public void rewardAspect(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        Reward annotation = method.getAnnotation(Reward.class);
        String value = annotation.value();
        Object[] args = joinPoint.getArgs();
        List<Object> objects = Arrays.asList(args);
        if(args != null){
            if(value.equals(Constant.COMPANY_INTRODUCTION)) {
                objects.stream()
                        .forEach(e -> {
                            if (e instanceof CompanyVo) {
                                Long memberId = ((CompanyVo) e).getMemberId();
                                String brief = ((CompanyVo) e).getBrief();
                                if(brief == null){
                                    MemberUserEntity memberUserEntity = memberUserService.selectById(memberId);
                                    int i = billService.selectCount(new EntityWrapper<BillEntity>()
                                            .eq("bus_business_id", ((CompanyVo) e).getMemberId())
                                            .eq("bill_title", "编写公司简介，增加大家对公司的了解"));
                                    if (i == 0) {
                                        BillEntity billEntity = new BillEntity();
                                        billEntity.setBillTitle("编写公司简介，增加大家对公司的了解");
                                        billEntity.setExpenditure(1);
                                        billEntity.setType(1);
                                    }
                                }
                            }
                        });
            }else if(value.equals(Constant.COMPANY_BUSINESS)){
                objects.stream()
                        .forEach(e -> {
                            if (e instanceof CompanyVo) {
                                Long memberId = ((CompanyVo) e).getMemberId();
                                String imageBusinessLicense = ((CompanyVo) e).getImageBusinessLicense();
                                if(imageBusinessLicense == null){
                                    MemberUserEntity memberUserEntity = memberUserService.selectById(memberId);
                                    int i = billService.selectCount(new EntityWrapper<BillEntity>()
                                            .eq("bus_business_id", ((CompanyVo) e).getMemberId())
                                            .eq("bill_title", "上传公司的营业执照，保障企业的合法性"));
                                    if (i == 0) {
                                        BillEntity billEntity = new BillEntity();
                                        billEntity.setBillTitle("上传公司的营业执照，保障企业的合法性");
                                        billEntity.setExpenditure(1);
                                        billEntity.setType(1);
                                    }
                                }
                            }
                        });
            }
        }
    }
}
